package io.modifier.basic.compose

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalView
import io.modifier.basic.compose.animation.LoadingSpinner
import io.modifier.basic.emo.modal.emoToast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

@Composable
fun TipWatcher() {
    val view = LocalView.current

    LaunchedEffect(Unit) {
        TipNotifier.tipEffect.collect {
            if (it.isNotBlank()) withContext(Dispatchers.Main) { view.emoToast(it, duration = 2100L).show() }
        }
    }
}

@Composable
fun LoadingWatcher() {
    val view = LocalView.current

    val showLoading = TipNotifier.loadingEffect.collectAsState(false).value

    if (showLoading) FullLoadingWatcher()
}

@Composable
fun FullLoadingWatcher() {
    Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { LoadingSpinner() }
}

